﻿<?php

require_once( ROOT_API . 'properties.inc.php');
require_once( ROOT_CLASS . 'Kernel.class.php');
require_once( ROOT_DATA_OBJECT . 'DataObject.class.php');
require_once( ROOT_DATA_ACCESS_OBJECT . 'DataAccessObject.class.php');



class UsuarioDO extends DataObject {
	var $table = "Usuario";
	var $attributes = array(
		"codigo"=> array("name" => "codigo","key" => "true", "type" => "int", "length" => "10", "null" => "NO", "values" => "", "default" => "0", "label" => "Codigo", "data" => "NULL"),
		"nome"=> array("name" => "nome","key" => "false", "type" => "string", "length" => "50", "null" => "YES", "values" => "", "default" => "0", "label" => "Codigo", "data" => "NULL"),
		"email"=> array("name" => "email","key" => "false", "type" => "string", "length" => "150", "null" => "YES", "values" => "", "default" => "0", "label" => "Codigo", "data" => "NULL"),
		"senha"=> array("name" => "senha","key" => "false", "type" => "string", "length" => "12", "null" => "YES", "values" => "", "default" => "0", "label" => "Codigo", "data" => "NULL"),
		"situacao"=> array("name" => "situacao","key" => "false", "type" => "string", "length" => "2", "null" => "YES", "values" => "", "default" => "0", "label" => "Codigo", "data" => "NULL"),
	);
	function UsuarioDO( $codigo = 'NULL' ){
		$this->set("codigo", $codigo);
	}
	function getNewInstance(){
		return new UsuarioDO();
	}
}

class AccessAction extends Kernel{

	function login( $instanceDB, $usuarioDO ){
		
		
		$prop = new Properties();
		$prop->add("Fields", "codigo, nome, email, senha, situacao");
		$prop->add("Conditions", "AND");

		$dao = new DataAccessObject();
		$resultado = $dao->find( $instanceDB, $usuarioDO, $prop );
		$aux = "";
		$result = "";
		
		if ( count($resultado) == 1 ){
			$this->criarSessaoUsuario( $resultado[0] );
			return TRUE;
		}
		return FALSE;
	}

	function capturar( $instanceDB, $usuarioDO ){
		$prop = new Properties();
		$prop->add("Fields", "codigo, nome, email, senha, situacao");
		$prop->add("Conditions", "AND");

		$dao = new DataAccessObject();
		$resultado = $dao->find( $instanceDB, $usuarioDO, $prop );
		$aux = "";
		$result = "";
		
		if ( count($resultado) == 1 ){
			return $resultado[0];
		}
		return FALSE;
	}

	function criarSessaoUsuario( $usuarioDO ){
		$_SESSION["usuario"] = $usuarioDO;
	}
	
	function capturarSessaoUsuario(){
		return $_SESSION["usuario"];
	}
	
	function isUserSession(){
		return session_is_registered ( "usuario" );
	}
	function logoff(){
		session_destroy();
	}
	
	function AccessAction(){
		if ( !$this->isUserSession() ){
			@session_start();
		}
	}
	
	function validSessionUsuario(){
		if ( !$this->isUserSession() ){
			@Header("Location: " . HTTP_SERVER . "/login.php");
		}
	}
	
}

?>